package com.redis.distributed.controller;

import com.redis.distributed.service.RedisService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import static java.lang.Thread.sleep;

@RestController
public class DistributeLock {
    @Autowired
    RedisService redisService;

    @GetMapping("/execute")
    public String execute(@RequestParam(defaultValue = "100") int timeOut,@RequestParam(defaultValue = "100")int runtime){
        boolean re= redisService.redisLock(timeOut,()->{
            sleep(runtime);
            return true;
        });
        return "执行"+(re?"成功":"执行失败");
    }
}
